package com.gzx.plugin.tjzy.modular.excel.template.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gzx.plugin.tjzy.modular.excel.template.bo.GzxExcelTemplateAddBo;
import com.gzx.plugin.tjzy.modular.excel.template.bo.GzxExcelTemplateQueryBo;
import com.gzx.plugin.tjzy.modular.excel.template.entity.GzxExcelTemplate;
import com.gzx.plugin.tjzy.modular.excel.template.mapper.GzxExcelTemplateMapper;
import com.gzx.plugin.tjzy.modular.excel.template.service.GzxExcelTemplateService;
import com.gzx.plugin.tjzy.modular.excel.template.vo.GzxExcelTemplateVo;
import com.gzx.plugin.tjzy.modular.file.consts.GzxFileBucketConstant;
import com.gzx.plugin.tjzy.modular.file.service.FileService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import vip.xiaonuo.common.exception.CommonException;

import java.io.IOException;
import java.util.List;

/**
 * @author zjt
 * @description
 * @date 2025/5/9 17:16
 */
@Service
@RequiredArgsConstructor
public class GzxExcelTemplateServiceImpl implements GzxExcelTemplateService {

    private final GzxExcelTemplateMapper excelTemplateMapper;

    private final FileService fileService;

    /**
     * 分页查询
     *
     * @param excelTemplateQueryBo
     * @return
     */
    @Override
    @DS("tjzy")
    public Page<GzxExcelTemplateVo> pageList(GzxExcelTemplateQueryBo excelTemplateQueryBo) {
        return excelTemplateMapper.selectVoPage(new Page<>(excelTemplateQueryBo.getCurrent(), excelTemplateQueryBo.getSize()), Wrappers.<GzxExcelTemplate>lambdaQuery()
                .orderByDesc(GzxExcelTemplate::getId));
    }

    /**
     * 根据类型获取模版
     *
     * @param type
     * @return
     */
    @Override
    @DS("tjzy")
    public List<GzxExcelTemplateVo> getTemplateListByType(Integer type) {
        return excelTemplateMapper.selectVoList(Wrappers.<GzxExcelTemplate>lambdaQuery().eq(GzxExcelTemplate::getType, type));
    }

    /**
     * 模版上传
     *
     * @param excelTemplateAddBo
     * @param multipartFile
     */
    @Override
    @DS("tjzy")
    public void upload(GzxExcelTemplateAddBo excelTemplateAddBo, MultipartFile multipartFile) throws IOException {
        GzxExcelTemplate excelTemplate = excelTemplateMapper.selectOne(Wrappers.<GzxExcelTemplate>lambdaQuery().eq(GzxExcelTemplate::getTemplateName, excelTemplateAddBo.getTemplateName()));
        if (excelTemplate != null) {
            throw new CommonException("模版已存在");
        }
        String fileName = fileService.upload(GzxFileBucketConstant.EXCEL_TEMPLATE, multipartFile, false);
        excelTemplate = new GzxExcelTemplate();
        excelTemplate.setType(excelTemplateAddBo.getType());
        excelTemplate.setTemplateName(excelTemplateAddBo.getTemplateName());
        excelTemplate.setFileName(fileName);
        excelTemplateMapper.insert(excelTemplate);
    }

    /**
     * 删除模版
     *
     * @param id
     */
    @Override
    @DS("tjzy")
    public void del(Long id) {
        GzxExcelTemplate excelTemplate = excelTemplateMapper.selectById(id);
        fileService.delete(GzxFileBucketConstant.EXCEL_TEMPLATE, excelTemplate.getFileName());
        excelTemplateMapper.deleteById(id);
    }
}
